import request from '@/utils/request'

export const userApi = {
  // 获取用户列表
  getUsers(params) {
    return request.get('/users', { params })
  },
  
  // 获取用户详情
  getUserById(id) {
    return request.get(`/users/${id}`)
  },
  
  // 创建用户
  createUser(data) {
    return request.post('/users', data)
  },
  
  // 更新用户
  updateUser(id, data) {
    return request.put(`/users/${id}`, data)
  },
  
  // 删除用户
  deleteUser(id) {
    return request.delete(`/users/${id}`)
  },
  
  // 批量删除用户
  batchDeleteUsers(ids) {
    return request.delete('/users/batch', { data: { ids } })
  },
  
  // 重置用户密码
  resetPassword(id, newPassword) {
    return request.put(`/users/${id}/password`, { password: newPassword })
  },
  
  // 启用/禁用用户
  toggleUserStatus(id, status) {
    return request.put(`/users/${id}/status`, { status })
  },

  // 获取用户统计信息
  getUserStats() {
    return request.get('/users/stats')
  },

  // 获取用户角色列表
  getUserRoles() {
    return request.get('/users/roles')
  },

  // 分配用户角色
  assignRole(userId, roleId) {
    return request.post(`/users/${userId}/roles`, { roleId })
  },

  // 移除用户角色
  removeRole(userId, roleId) {
    return request.delete(`/users/${userId}/roles/${roleId}`)
  },

  // 获取用户权限
  getUserPermissions(userId) {
    return request.get(`/users/${userId}/permissions`)
  },

  // 搜索用户
  searchUsers(keyword, filters = {}) {
    return request.get('/users/search', { 
      params: { keyword, ...filters } 
    })
  },

  // 导出用户数据
  exportUsers(format = 'excel', filters = {}) {
    return request.get('/users/export', {
      params: { format, ...filters },
      responseType: 'blob'
    })
  },

  // 导入用户数据
  importUsers(file) {
    const formData = new FormData()
    formData.append('file', file)
    return request.post('/users/import', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  },

  // 获取用户活动日志
  getUserLogs(userId, params = {}) {
    return request.get(`/users/${userId}/logs`, { params })
  },

  // 获取在线用户列表
  getOnlineUsers() {
    return request.get('/users/online')
  },

  // 强制用户下线
  forceLogout(userId) {
    return request.post(`/users/${userId}/logout`)
  },

  // 获取用户登录历史
  getLoginHistory(userId, params = {}) {
    return request.get(`/users/${userId}/login-history`, { params })
  },

  // 验证用户名是否可用
  checkUsername(username) {
    return request.get('/users/check-username', { 
      params: { username } 
    })
  },

  // 验证邮箱是否可用
  checkEmail(email) {
    return request.get('/users/check-email', { 
      params: { email } 
    })
  },

  // 发送密码重置邮件
  sendPasswordResetEmail(email) {
    return request.post('/users/send-reset-email', { email })
  },

  // 通过令牌重置密码
  resetPasswordByToken(token, newPassword) {
    return request.post('/users/reset-password', { token, password: newPassword })
  },

  // 获取用户头像上传地址
  getAvatarUploadUrl(userId) {
    return request.get(`/users/${userId}/avatar-upload-url`)
  },

  // 更新用户头像
  updateAvatar(userId, avatarFile) {
    const formData = new FormData()
    formData.append('avatar', avatarFile)
    return request.post(`/users/${userId}/avatar`, formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  }
}
